﻿using KPIS.GERP.Models.ICM;
using System;
using System.Linq;
using System.Web.Mvc;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class MasPurchaseRegularController : Controller
    {
        #region MasPurchaseRegular
        public ActionResult Index()
        {
            var service = new IcmMasterService.IcmMasterServiceClient();

            return View(service.SelectPurchaseRegular(new Models.ICM.MasPurchaseRegular { RecordStatus = "!D" }, null));
        }

        public ActionResult Details(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseRegular(new Models.ICM.MasPurchaseRegular { RegularId = id }, null).FirstOrDefault();

            return View(model);
        }

        public ActionResult Create()
        {
            
            return View(new KPIS.GERP.Models.ICM.MasPurchaseRegular());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MasPurchaseRegular model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    var id = service.InsertPurchaseRegular(model, Class.GimsMembership.CurrentEmpId);
                    
                    return RedirectToAction("Details", new { id = id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseRegular(new Models.ICM.MasPurchaseRegular { RegularId = id }, null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, MasPurchaseRegular model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    service.UpdatePurchaseRegular(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { id = id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new IcmMasterService.IcmMasterServiceClient();
                service.DeletePurchaseRegular(id, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeletePurchaseRegular(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }
        #endregion

        #region MasPurchaseRegularMapping
        public ActionResult IndexMapping(int RegularId)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();

            return View(service.SelectPurchaseRegularMapping(new Models.ICM.MasPurchaseRegularMapping { RegularId = RegularId, RecordStatus = "!D" }, new[] { "MasPurchaseType", "Priority" }));
        }

        public ActionResult DetailsMapping(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseRegularMapping(new Models.ICM.MasPurchaseRegularMapping { RegularMapId = id }, new[] { "MasBudgetSource" }).FirstOrDefault();

            return View(model);
        }

        public ActionResult CreateMapping(int RegularId)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var modelRegular = service.SelectPurchaseRegular(new Models.ICM.MasPurchaseRegular { RegularId = RegularId }, null).FirstOrDefault();
            var model = new MasPurchaseRegularMapping();
            model.RegularId = modelRegular.RegularId;

            return PartialView(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateMapping(MasPurchaseRegularMapping model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.RegularMapId = -1;
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    var id = service.InsertPurchaseRegularMapping(model, Class.GimsMembership.CurrentEmpId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }

            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
                //throw ex;
            }

            return Json(new { result = false, message = KPIS.GERP.Models.Class.ValidateText.RequiredField, alertClass = "" });
        }

        public ActionResult EditMapping(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseRegularMapping(new Models.ICM.MasPurchaseRegularMapping { RegularMapId = id }, null).FirstOrDefault();

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditMapping(int id, MasPurchaseRegularMapping model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    service.UpdatePurchaseRegularMapping(model, Class.GimsMembership.CurrentEmpId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
                //throw ex;
            }

            return Json(new { result = false, message = KPIS.GERP.Models.Class.ValidateText.RequiredField, alertClass = "" });
        }

        public ActionResult DeleteMapping(int id)
        {
            try
            {
                var service = new IcmMasterService.IcmMasterServiceClient();
                var RegularId = service.SelectPurchaseRegularMapping(new Models.ICM.MasPurchaseRegularMapping { RegularMapId = id }, null).FirstOrDefault().RegularId;
                service.DeletePurchaseRegularMapping(id, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Details", new { id = RegularId });
            }
            catch
            {
                var service = new IcmMasterService.IcmMasterServiceClient();
                return View("Details", new { id = service.SelectPurchaseRegularMapping(new Models.ICM.MasPurchaseRegularMapping { RegularMapId = id }, null).FirstOrDefault().RegularId.GetValueOrDefault(0) });
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAllMapping(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeletePurchaseRegularMapping(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }
        #endregion
    }
}
